package cn.siriusbot.siriuspro.admin.webapi;

import cn.siriusbot.siriuspro.admin.config.database.DatabaseManage;
import cn.siriusbot.siriuspro.error.MsgException;
import cn.siriusbot.siriuspro.web.R.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 引导控制层
 */
@RestController
@RequestMapping("/api/guide")
public class GuideController {

    @Autowired
    DatabaseManage databaseManage;


    /**
     * 是否第一次引导
     */
    @RequestMapping("/first-open")
    public R getFirstOpen() {
        return new R()
                .setData(databaseManage.firstOpen());
    }

    /**
     * 测试连接
     */
    @RequestMapping("/test-connect")
    public R testConnectionMysql(
            String url,
            String database,
            String username,
            String password
    ) {
        if (!databaseManage.firstOpen()) {
            throw new MsgException(500, "当前无法初始化数据，如需初始化，请删除'/conf/database.properties'文件");
        }
        if (ObjectUtils.isEmpty(url)) {
            throw new MsgException(500, "连接地址不能为空");
        }
        if (ObjectUtils.isEmpty(database)) {
            throw new MsgException(500, "数据库名不能为空");
        }
        if (ObjectUtils.isEmpty(username)) {
            throw new MsgException(500, "数据库用户不能为空");
        }
        if (ObjectUtils.isEmpty(password)) {
            throw new MsgException(500, "数据库密码不能为空");
        }
        final boolean b = databaseManage.connectMysql(url, database, username, password);
        return new R()
                .setCode(b ? 0 : 500)
                .setMsg(b ? "连接成功" : "连接失败");
    }


    /**
     * 导入数据库
     */
    @RequestMapping("/import-database")
    public R importDatabase() {
        if (!databaseManage.firstOpen()) {
            throw new MsgException(500, "当前无法初始化数据，如需初始化，请删除'/conf/database.properties'文件");
        }
        databaseManage.importMysqlData();
        return new R()
                .setMsg("导入表数据成功!");
    }


    /**
     * 设置mysql数据库并设置管理员账号密码
     */
    @RequestMapping("/set-mysql")
    public R setMysql(
            String account,
            String passwd
    ) {
        if (!databaseManage.firstOpen()) {
            throw new MsgException(500, "当前无法初始化数据，如需初始化，请删除'/conf/database.properties'文件");
        }
        if (ObjectUtils.isEmpty(account)) {
            throw new MsgException(500, "管理员账号不能为空");
        }
        if (ObjectUtils.isEmpty(passwd)) {
            throw new MsgException(500, "管理员密码不能为空");
        }
        // 设置账户密码
        databaseManage.mysqlAddAdmin(account, passwd);
        // 将配置文件写入到文件
        final String mysqlInfo = databaseManage.getMysqlInfo();
        final String dir = System.getProperty("user.dir");
        try {
            Files.writeString(Path.of(dir + "/conf/database.properties"), mysqlInfo);
        } catch (IOException e) {
            e.printStackTrace();
            throw new MsgException(500, "配置写入文件出错!");
        }
        // 设置当前环境数据库为mysql
        databaseManage.setType(DatabaseManage.Type.Mysql);
        databaseManage.setFirstOpen(false);
        new Thread(() -> {
            try {
                // 3秒后关闭框架
                Thread.sleep(3000);
                System.exit(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        return new R()
                .setMsg("配置成功，需要重启框架才可生效");
    }

    /**
     * 设置mysql数据库并设置管理员账号密码
     */
    @RequestMapping("/set-sqlite")
    public R setSqlite(
            String account,
            String passwd
    ) {
        if (!databaseManage.firstOpen()) {
            throw new MsgException(500, "当前无法初始化数据，如需初始化，请删除'/conf/database.properties'文件");
        }
        if (ObjectUtils.isEmpty(account)) {
            throw new MsgException(500, "管理员账号不能为空");
        }
        if (ObjectUtils.isEmpty(passwd)) {
            throw new MsgException(500, "管理员密码不能为空");
        }
        // 设置账户密码
        databaseManage.sqliteAddAdmin(account, passwd);
        // 将配置文件写入到文件
        final String dir = System.getProperty("user.dir");
        try {
            Files.writeString(Path.of(dir + "/conf/database.properties"), "type = sqlite");
        } catch (IOException e) {
            e.printStackTrace();
            throw new MsgException(500, "配置写入文件出错!");
        }
        // 设置当前环境数据库为Sqlite
        databaseManage.setType(DatabaseManage.Type.Sqlite);
        databaseManage.setFirstOpen(false);
        return new R()
                .setMsg("配置成功");
    }

}
